﻿using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using UpperComputer.Core.Extensions;
using System;
using System.Collections.Generic;
using YFurion.Entities;
using YFurion.Utils;

namespace UpperComputer.Core.Entities
{
    public class BaseUser : FullEntity, IEntityTypeBuilder<BaseUser>, IEntitySeedData<BaseUser>
    {
        public BaseUser()
        {
            BaseRoleUser = new HashSet<BaseRoleUser>();
        }

        public string Account { get; set; }

        public string DisplayName { get; set; }

        public byte Sex { get; set; }

        public string Password { get; set; }

        public string Photo { get; set; }

        public bool IsActive { get; set; }

        public string Description { get; set; }

        public virtual ICollection<BaseRoleUser> BaseRoleUser { get; set; }

        public void Configure(EntityTypeBuilder<BaseUser> entity, DbContext dbContext, Type dbContextLocator)
        {
            entity.HasIndex(e => e.Account, "UQ__BaseUser")
                    .IsUnique();

            entity.Property(e => e.Id)
                .ValueGeneratedNever()
                .HasComment("编号");

            entity.Property(e => e.Account)
                .IsRequired()
                .HasMaxLength(50)
                .IsUnicode(false)
                .HasComment("账号");

            entity.Property(e => e.CreationTime).HasComment("创建时间");

            entity.Property(e => e.CreatorUserId)
                .HasComment("创建者编号");

            entity.Property(e => e.DeleterUserId)
                .HasComment("删除者编号");

            entity.Property(e => e.DeletionTime).HasComment("删除时间");

            entity.Property(e => e.Description)
                .HasMaxLength(255)
                .HasComment("描述");

            entity.Property(e => e.DisplayName)
                .IsRequired()
                .HasMaxLength(50)
                .HasComment("显示名称");

            entity.Property(e => e.IsActive).HasComment("是否激活");

            entity.Property(e => e.IsDeleted).HasComment("是否删除");

            entity.Property(e => e.LastModificationTime).HasComment("最后一次修改时间");

            entity.Property(e => e.LastModifierUserId)
                .HasComment("最后一次修改者编号");

            entity.Property(e => e.Password)
                .IsRequired()
                .HasMaxLength(50)
                .IsUnicode(false)
                .HasComment("密码");

            entity.Property(e => e.Photo)
                .HasMaxLength(100)
                .HasComment("照片");
        }

        public IEnumerable<BaseUser> HasData(DbContext dbContext, Type dbContextLocator)
        {
            long id = YIDGenerator.NextId();
            return new List<BaseUser>
            {
                new BaseUser
                {
                    Id = id,
                    Account = "Admin",
                    DisplayName = "超级管理员",
                    Password = "123456".GetMD5SuffixPassword().GetMD5PrefixPassword(),
                    CreationTime = DateTime.Now,
                    CreatorUserId = id,
                    IsActive = true,
                    Sex = 0,
                    Description = ""
                }
            };
        }
    }
}